package com.touringapp.android.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


import com.touringapp.android.R;
import com.touringapp.android.utils.LocationUtils;

public class MainActivity extends Activity{

	private static final int OPEN_SETTINGS = 1;

	public static String userName;
	
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		if (requestCode==OPEN_SETTINGS) {
			SharedPreferences prefs = PreferenceManager.
						getDefaultSharedPreferences(this);
			userName = prefs.getString("edittext_preference",null);
			if  (userName==null|| userName.equals("")) {
				Toast.makeText(this, R.string.error_no_username, Toast.LENGTH_LONG).show();
			}
			updateName();
		}
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
		userName = prefs.getString("edittext_preference",null);
		if  (userName==null || userName.equals("")) {
			Intent intent = new Intent(this,SettingsActivity.class);
			startActivityForResult(intent, OPEN_SETTINGS);
		}
		
		updateName();
		
		if (LocationUtils.isStarted()) {
			((Button)findViewById(R.id.service_button)).setText(R.string.service_button_text_stop);
		}else{
			((Button)findViewById(R.id.service_button)).setText(R.string.service_button_text_start);
		}
		
		Button serviceButton = (Button)findViewById(R.id.service_button);
		serviceButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				toggleService();
			}
		});
		
		Button peopleButton = (Button)findViewById(R.id.people_list);
		peopleButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getApplicationContext(),UserListActivity.class);
				startActivity(intent);
			}
		});
	}

	private void updateName() {
		// TODO Auto-generated method stub
		String usernameLabel = getString(R.string.username_label);
		if (userName==null || userName.equals("")) {
			usernameLabel = getString(R.string.goto_settings);
		}else{
			usernameLabel =usernameLabel + " "+ userName;
		}
		((TextView)findViewById(R.id.username)).setText(usernameLabel);
		
	}

	protected void toggleService() {
		// TODO Auto-generated method stub
		
		
		if (LocationUtils.isStarted()) {
			LocationUtils.stop(getApplicationContext());
			((Button)findViewById(R.id.service_button)).setText(R.string.service_button_text_start);
		}else{
			LocationUtils.start(getApplicationContext(), 30000);
			((Button)findViewById(R.id.service_button)).setText(R.string.service_button_text_stop);
		}
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
		
		
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.preference_menu_item: 
			Intent intent = new Intent(MainActivity.this,SettingsActivity.class);
			startActivity(intent);
		}
		return super.onOptionsItemSelected(item);
	}
	
	
	

}
